package com.hh.system.action;

import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.hh.baseframework.util.io.FileUtil;
import com.hh.system.inf.IImageAction;
import com.hh.system.util.base.BaseServletAction;

@SuppressWarnings("serial")
public class ImageAction extends BaseServletAction implements IImageAction {
	private byte[] bytes = null;
	private String contentType = "image/";
	private String path;

	public String image() {
		InputStream inputStream = getClass().getClassLoader()
				.getResourceAsStream(path);
		long clientLastModified = request.getDateHeader("If-Modified-Since");
		if (clientLastModified > 100000) {
			response.setStatus(304);
			return null;
		} else {
			response.addDateHeader("Last-Modified", System.currentTimeMillis());
			response.setHeader("Cache-Control", "public");
		}
		this.setContentType(contentType
				+ path.substring(path.lastIndexOf(".") + 1));
		this.setBytes(FileUtil.stream2Byte(inputStream));
		return "image";
	}

	public byte[] getBytes() {
		return bytes;
	}

	public void setBytes(byte[] bytes) {
		this.bytes = bytes;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public int getContentLength() {
		return this.bytes.length;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

}
